<template>
  <div ref="target">
    <HomePanel title="人气推荐" sub-title="人气爆款 不容错过">
      <div class="box">
        <transition name="fade">
          <ul ref="pannel" class="goods-list" v-if="list.length">
            <li v-for="item in list" :key="item.id">
              <RouterLink :to="'/product/' + item.id">
                <img :src="item.picture" alt="" />
                <p class="name">{{ item.title }}</p>
                <p class="desc">{{ item.alt }}</p>
              </RouterLink>
            </li>
          </ul>
          <HomeSkeleton v-else></HomeSkeleton>
        </transition>
      </div>
    </HomePanel>
  </div>
</template>

<script>
/*
1:先写好骨架屏结构 
2：做好v-if与v-else处理
3：加入动画
*/
/*
懒加载:
  1:相应dom进入可视区域才开始加载，
  2：加载过一次后，再次进入可视区域就不再加载了

*/

// import { ref } from 'vue'
import HomePanel from './home-panel'
import { homeHot } from '@/api/home'
import HomeSkeleton from './home-skeleton.vue'
// import { useIntersectionObserver } from '@vueuse/core'
import { lazyLoad } from '@/utils/index'
export default {
  name: 'HomeHot',
  components: { HomePanel, HomeSkeleton },
  setup () {
    // const list = ref([])
    // 懒加载
    // const target = ref(null)
    // const { stop } = useIntersectionObserver(
    //   target,
    //   ([{ isIntersecting }], elementOption) => {
    //     if (isIntersecting) {
    //       console.log('进入可视区域')
    //       stop()
    //       homeHot().then(res => {
    //         list.value = res.result
    //       })
    //     }
    //   },
    //   {
    //     threshold: 0
    //   }
    // )
    const { list, target } = lazyLoad(homeHot)
    return { list, target }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
.box {
  position: relative;
  height: 426px;
}
</style>
